1 using UnityEngine;
2 using System.Collections;
3
4 public class BlockScript : MonoBehaviour {
5
6 public int x; //index along x axis
7 public int y; //index along y axis
8 public int z; //index along z axis
9 public int blockNumber; //number displayed on the block (-1 for no number)
10 public Vector3 originalPosition; //orginal position of the block
11 private int moveNewBlockNumber; //number to change to once the move has been completed
12 private float moveStartTime = -1F; //amount of seconds since move started
13 private Vector3 rotateDirection;
14 private float rotationTotal = -1F;
15 private int rotateNewBlockNumber;
16 private Vector3 moveNewPosition; //final destination of move
17 private float moveDuration = 0.1F;
18 private float scale = 0;
19 private float yOffset;
20 private GameControllerScript gameScript;
21
22 public static int emptyBlock = -1;
23 public static int voidBlock = -2;
24
25
26 public void Initialize(int x, int y, int z, GameControllerScript gameScript) {
27 this.scale = gameScript.scale;
28 this.yOffset = gameScript.yOffset;
29 this.gameScript = gameScript;
30 this.x = x;
31 this.y = y;
32 this.z = z;
33 this.originalPosition = new Vector3(x * this.scale, y * this.scale + this.yOffset, z * this.scale);
34 }
35
36 // Use this for initialization
37 void Start () {
38 //this.originalPosition = transform.position;
39 }
40
41 // Update is called once per frame
42 void Update () {
43
44 if (moveStartTime >= 0F) {
45 float percentComplete = (Time.time - this.moveStartTime) / this.moveDuration;
46 if(this.blockNumber > -1) {
47 transform.position = Vector3.Lerp(this.originalPosition, this.moveNewPosition, percentComplete);
48 }
49 if (percentComplete >= 1F) {
50 this.moveStartTime = -1F;
51 this.setBlockNumber(this.moveNewBlockNumber);
52 transform.position = this.originalPosition;
53 }
54 }
55
56 if (this.rotationTotal >= 0F) {
57 float rotateBy = 90 * Time.deltaTime / this.moveDuration;
58 Vector3 rotationPoint = new Vector3 (3f, 4f, 3f);
59 transform.RotateAround(rotationPoint, this.rotateDirection, rotateBy);
60 this.rotationTotal += rotateBy;
61
62 if(rotationTotal >= 90) {
63 Debug.Log (rotationTotal);
64 this.rotationTotal = -1F;
65 transform.position = this.originalPosition;
66 transform.rotation = new Quaternion(0,0,0,1);
67 this.setBlockNumber(rotateNewBlockNumber);
68 }
69 }
70 }
71
72 public void move(string axis, int units, float scale, int newBlockNumber) {
73
74 //this.moveDuration = duration;
75 this.scale = scale;
76 this.moveNewBlockNumber = newBlockNumber;
77 this.moveStartTime = Time.time;
78 this.moveNewPosition = this.originalPosition;
79 //this.transform = this.originalPosition;
80 if (axis == "x")
81 this.moveNewPosition.x = this.originalPosition.x + (this.scale * units);
82 if (axis == "y")
83 this.moveNewPosition.y = this.originalPosition.y + (this.scale * units);
84 if (axis == "z")
85 this.moveNewPosition.z = this.originalPosition.z + (this.scale * units);
86 }
87
88 public void rotateBlock (Vector3 direction) {
89 this.rotateDirection = direction;
90 this.rotationTotal = 0;
91 this.rotateNewBlockNumber = this.GetNumberAfterRotation(direction);
92 }
93
94 public void setBlockNumber(int blockNumber) {
95 this.blockNumber = blockNumber;
96 TextMesh textMesh = this.GetComponentInChildren<TextMesh>();
97 Material blockTextMaterial = gameObject.transform.Find ("BlockText").gameObject.GetComponent<Renderer>().material;
98 MeshRenderer cube = gameObject.GetComponentInChildren<MeshRenderer>();
99 Color cubeColor = cube.GetComponent<Renderer>().material.color;
100
101 //block doesn't exits
102 if(blockNumber == -2) {
103 cube.GetComponent<Renderer>().enabled = false;
104 textMesh.text = "";
105 }
106 //block is empty
107 else if (blockNumber == -1 ) {
108 cube.GetComponent<Renderer>().enabled = false;
109 cube.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0.2f);
110 textMesh.text = "";
111 }
112 //block has a value
113 else {
114 cube.GetComponent<Renderer>().enabled = true;
115 cube.material.color = this.getColor (blockNumber);
116 blockTextMaterial.SetColor ("_Color", new Color(1,1,1));
117 if(blockNumber == 0) blockTextMaterial.SetColor ("_Color", new Color(0.8f,0.8f,1));
118 textMesh.text = blockNumber.ToString();
119 transform.position = this.originalPosition;
120 }
121
122 }
123
124 private Color getColor(int num) {
125 if (num == -2) return HexToColor ("000000");
126 if (num == 0) return HexToColor ("000055");
127 if (num == 2) return HexToColor ("3333cc");
128 if (num == 4) return HexToColor ("0099aa");
129 if (num == 8) return HexToColor ("00ff99");
130 if (num == 16) return HexToColor ("00ff00");
131 if (num == 32) return HexToColor ("99ff00");
132 if (num == 64) return HexToColor ("bbff55");
133 if (num == 128) return HexToColor ("ffff00");
134 if (num == 256) return HexToColor ("ff9933");
135 if (num == 512) return HexToColor ("ff6600");
136 if (num == 1024) return HexToColor ("ff5050");
137 if (num == 2048) return HexToColor ("ff0000");
138 if (num == 4096) return HexToColor ("cc0066");
139 if (num == 8192) return HexToColor ("990099");
140 if (num == 16284) return HexToColor ("9999ff");
141 if (num == 32568) return HexToColor ("ffffff");
142 return new Color (0.5f, 0.5f, 0.5f);
143
144
145 }
146
147 Color HexToColor(string hex)
148 {
149 byte r = byte.Parse(hex.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
150 byte g = byte.Parse(hex.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
151 byte b = byte.Parse(hex.Substring(4,2), System.Globalization.NumberStyles.HexNumber);
152 return new Color32(r,g,b, 255);
153 }
154
155 private int GetNumberAfterRotation(Vector3 direction) {
156 //rotating to the left
157 if(direction == Vector3.up) {
158 return gameScript.getBlockNumber (2-z,y,x);
159 }
160 //rotating to the right
161 if(direction == Vector3.down) {
162 return gameScript.getBlockNumber (z,y,2-x);
163 }
164 //rotating up
165 if(direction == Vector3.left) {
166 return gameScript.getBlockNumber (x,2-z,y);
167 }
168 //rotating down
169 if(direction == Vector3.right) {
170 return gameScript.getBlockNumber (x,z,2-y);
171 }
172 return 1;
173 }
174 }
public int x; index along x axis
public int y; index along y axis
public int z; index along z axis
public int blockNumber; number displayed on the block (-1 for no number)
public Vector3 originalPosition; orginal position of the block
private int moveNewBlockNumber; number to change to once the move has been completed
private float moveStartTime = -1F; amount of seconds since move started
private Vector3 moveNewPosition; final destination of move
Use this for initialization
this.originalPosition = transform.position;
Update is called once per frame
this.moveDuration = duration;
this.transform = this.originalPosition;
block doesn't exits
block is empty
block has a value
rotating to the left
rotating to the right
rotating up
rotating down